/*     */ package com.zimbra.qa.unittest.prov.ldap;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Provisioning.ProvisioningValidator;
/*     */ import com.zimbra.cs.account.ldap.ChangePasswordListener;
/*     */ import com.zimbra.cs.account.ldap.ChangePasswordListener.InternalChangePasswordListenerId;
/*     */ import com.zimbra.cs.account.ldap.LdapProv;
/*     */ import com.zimbra.qa.unittest.prov.ProvTest.Sequencer;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import org.junit.AfterClass;
/*     */ import org.junit.Assert;
/*     */ import org.junit.BeforeClass;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestChangePasswordListener
/*     */   extends LdapTest
/*     */ {
/*     */   private static LdapProvTestUtil provUtil;
/*     */   private static Domain domain;
/*  36 */   private static ProvTest.Sequencer passGen = new ProvTest.Sequencer();
/*     */   private static LdapProv prov;
/*     */   
/*     */   @BeforeClass
/*     */   public static void init()
/*     */     throws Exception
/*     */   {
/*  43 */     provUtil = new LdapProvTestUtil();
/*  44 */     prov = provUtil.getProv();
/*     */   }
/*     */   
/*     */   @AfterClass
/*     */   public static void cleanup() throws Exception {
/*  49 */     Cleanup.deleteAll(new String[] { baseDomainName() });
/*     */   }
/*     */   
/*     */   private String getZimbraDomainName(String testName)
/*     */   {
/*  54 */     return testName + "." + baseDomainName();
/*     */   }
/*     */   
/*     */   private Domain createZimbraDomain(String testName, Map<String, Object> zimbraDomainAttrs) throws Exception
/*     */   {
/*  59 */     return provUtil.createDomain(getZimbraDomainName(testName), zimbraDomainAttrs);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   @Test
/*     */   public void onExceptionAtPreModify()
/*     */     throws Exception
/*     */   {
/*  69 */     String testName = getTestName();
/*     */     
/*     */ 
/*  72 */     String className = TestListenerExceptionThrownAtPreModify.class.getName();
/*  73 */     TestListenerExceptionThrownAtPreModify listener = new TestListenerExceptionThrownAtPreModify();
/*  74 */     TestListenerExceptionThrownAtPreModify.register(className, listener);
/*     */     
/*     */ 
/*  77 */     Map<String, Object> zimbraDomainAttrs = new HashMap();
/*  78 */     zimbraDomainAttrs.put("zimbraPasswordChangeListener", className);
/*  79 */     domain = createZimbraDomain(getZimbraDomainName(testName), zimbraDomainAttrs);
/*     */     
/*     */ 
/*  82 */     String acctName = testName + "@" + domain.getDomainName();
/*  83 */     String oldpassword = "old_password_" + passGen.next();
/*  84 */     Account acct = provUtil.createAccount(acctName, oldpassword);
/*     */     
/*     */ 
/*  87 */     String newpassword = "new_password_" + passGen.next();
/*     */     try {
/*  89 */       prov.setPassword(acct, newpassword);
/*  90 */       Assert.fail();
/*     */     }
/*     */     catch (Exception e) {
/*  93 */       Assert.assertTrue(e.getMessage().equals(ServiceException.TEMPORARILY_UNAVAILABLE().getMessage()));
/*     */     }
/*     */     
/*     */ 
/*  97 */     Assert.assertNotNull(listener);
/*  98 */     Assert.assertTrue(listener.isPreModifyCalled().booleanValue());
/*  99 */     Assert.assertFalse(listener.isPostModifyCalled().booleanValue());
/*     */     
/* 101 */     Assert.assertFalse(listener.isOnExceptionCalled().booleanValue());
/*     */   }
/*     */   
/*     */ 
/*     */   public class TestListenerExceptionThrownAtPreModify
/*     */     extends ChangePasswordListener
/*     */   {
/*     */     private Boolean preModifyCalled;
/*     */     
/*     */     private Boolean postModifyCalled;
/*     */     
/*     */     private Boolean onExceptionCalled;
/*     */     
/*     */     private Account acctParam;
/*     */     private String newPasswordParam;
/*     */     private ServiceException exceptionThrown;
/*     */     
/*     */     public TestListenerExceptionThrownAtPreModify()
/*     */     {
/* 120 */       this.preModifyCalled = Boolean.valueOf(false);
/* 121 */       this.postModifyCalled = Boolean.valueOf(false);
/* 122 */       this.onExceptionCalled = Boolean.valueOf(false);
/* 123 */       this.acctParam = null;
/* 124 */       this.newPasswordParam = null;
/* 125 */       this.exceptionThrown = null;
/*     */     }
/*     */     
/*     */     public void preModify(Account acct, String newPassword, Map context, Map<String, Object> attrsToModify) throws ServiceException
/*     */     {
/* 130 */       this.preModifyCalled = Boolean.valueOf(true);
/*     */       
/* 132 */       this.acctParam = acct;
/* 133 */       this.newPasswordParam = newPassword;
/* 134 */       this.exceptionThrown = ServiceException.TEMPORARILY_UNAVAILABLE();
/* 135 */       throw this.exceptionThrown;
/*     */     }
/*     */     
/*     */ 
/*     */     public void postModify(Account acct, String newPassword, Map context)
/*     */     {
/* 141 */       this.postModifyCalled = Boolean.valueOf(true);
/*     */     }
/*     */     
/*     */     public void onException(Account acct, String newPassword, Map context, ServiceException se) {
/* 145 */       this.onExceptionCalled = Boolean.valueOf(true);
/* 146 */       Assert.fail();
/*     */     }
/*     */     
/*     */ 
/*     */     public Boolean isPreModifyCalled()
/*     */     {
/* 152 */       return this.preModifyCalled;
/*     */     }
/*     */     
/*     */     public Boolean isPostModifyCalled() {
/* 156 */       return this.postModifyCalled;
/*     */     }
/*     */     
/*     */     public Boolean isOnExceptionCalled() {
/* 160 */       return this.onExceptionCalled;
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   @Test
/*     */   public void onExceptionAtModifyAttrs01()
/*     */     throws Exception
/*     */   {
/* 170 */     String testName = getTestName();
/*     */     
/*     */ 
/* 173 */     String className = TestListenerExceptionThrownAtModifyAttrs.class.getName();
/* 174 */     TestListenerExceptionThrownAtModifyAttrs listener = new TestListenerExceptionThrownAtModifyAttrs();
/* 175 */     TestListenerExceptionThrownAtModifyAttrs.register(className, listener);
/*     */     
/*     */ 
/* 178 */     Map<String, Object> zimbraDomainAttrs = new HashMap();
/* 179 */     zimbraDomainAttrs.put("zimbraPasswordChangeListener", className);
/* 180 */     domain = createZimbraDomain(getZimbraDomainName(testName), zimbraDomainAttrs);
/*     */     
/*     */ 
/* 183 */     String acctName = testName + "@" + domain.getDomainName();
/* 184 */     String oldpassword = "old_password_" + passGen.next();
/* 185 */     Account acct = provUtil.createAccount(acctName, oldpassword);
/*     */     
/*     */ 
/* 188 */     DummyValidator dm = new DummyValidator();
/* 189 */     dm.activate();
/* 190 */     prov.register(dm);
/*     */     
/*     */ 
/* 193 */     String newpassword = "new_password_" + passGen.next();
/*     */     try {
/* 195 */       prov.setPassword(acct, newpassword);
/* 196 */       Assert.fail();
/*     */     }
/*     */     catch (Exception e) {
/* 199 */       Assert.assertTrue(e.getMessage().equals("forbidden: password modification is not permitted for this account"));
/*     */     }
/*     */     
/* 202 */     dm.inactivate();
/*     */     
/* 204 */     Assert.assertNotNull(listener);
/* 205 */     Assert.assertTrue(listener.isPreModifyCalled().booleanValue());
/* 206 */     Assert.assertFalse(listener.isPostModifyCalled().booleanValue());
/* 207 */     Assert.assertTrue(listener.isOnExceptionCalled().booleanValue());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   @Test
/*     */   public void onExceptionAtModifyAttrs02()
/*     */     throws Exception
/*     */   {
/* 217 */     String testName = getTestName();
/*     */     
/*     */ 
/* 220 */     String className = TestListenerExceptionThrownAtModifyAttrs.class.getName();
/* 221 */     TestListenerExceptionThrownAtModifyAttrs listener = new TestListenerExceptionThrownAtModifyAttrs();
/* 222 */     TestListenerExceptionThrownAtModifyAttrs.registerInternal(ChangePasswordListener.InternalChangePasswordListenerId.CPL_SYNC, listener);
/*     */     
/*     */ 
/* 225 */     Map<String, Object> zimbraDomainAttrs = new HashMap();
/* 226 */     domain = createZimbraDomain(getZimbraDomainName(testName), zimbraDomainAttrs);
/*     */     
/*     */ 
/* 229 */     String acctName = testName + "@" + domain.getDomainName();
/* 230 */     String oldpassword = "old_password_" + passGen.next();
/* 231 */     Account acct = provUtil.createAccount(acctName, oldpassword);
/*     */     
/*     */ 
/* 234 */     DummyValidator dm = new DummyValidator();
/* 235 */     dm.activate();
/* 236 */     prov.register(dm);
/*     */     
/*     */ 
/* 239 */     String newpassword = "new_password_" + passGen.next();
/*     */     try {
/* 241 */       prov.setPassword(acct, newpassword);
/* 242 */       Assert.fail();
/*     */     }
/*     */     catch (Exception e) {
/* 245 */       Assert.assertTrue(e.getMessage().equals("forbidden: password modification is not permitted for this account"));
/*     */     }
/*     */     
/* 248 */     dm.inactivate();
/*     */     
/* 250 */     Assert.assertNotNull(listener);
/* 251 */     Assert.assertTrue(listener.isPreModifyCalled().booleanValue());
/* 252 */     Assert.assertFalse(listener.isPostModifyCalled().booleanValue());
/* 253 */     Assert.assertTrue(listener.isOnExceptionCalled().booleanValue());
/*     */   }
/*     */   
/*     */ 
/*     */   public static class DummyValidator
/*     */     implements Provisioning.ProvisioningValidator
/*     */   {
/*     */     private boolean activated;
/*     */     
/*     */     public void validate(Provisioning prov, String action, Object... args)
/*     */       throws ServiceException
/*     */     {
/* 265 */       if (("modifyAccountCheckDomainCosAndFeature".equals(action)) && (this.activated)) {
/* 266 */         throw ServiceException.FORBIDDEN("password modification is not permitted for this account");
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */     public void refresh() {}
/*     */     
/*     */ 
/*     */     public void activate()
/*     */     {
/* 277 */       this.activated = true;
/*     */     }
/*     */     
/*     */ 
/*     */     public void inactivate()
/*     */     {
/* 283 */       this.activated = false;
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public class TestListenerExceptionThrownAtModifyAttrs
/*     */     extends ChangePasswordListener
/*     */   {
/*     */     private Boolean preModifyCalled;
/*     */     private Boolean postModifyCalled;
/*     */     private Boolean onExceptionCalled;
/*     */     private Account acctParam;
/*     */     private String newPasswordParam;
/*     */     
/*     */     public TestListenerExceptionThrownAtModifyAttrs()
/*     */     {
/* 299 */       this.preModifyCalled = Boolean.valueOf(false);
/* 300 */       this.postModifyCalled = Boolean.valueOf(false);
/* 301 */       this.onExceptionCalled = Boolean.valueOf(false);
/* 302 */       this.acctParam = null;
/* 303 */       this.newPasswordParam = null;
/*     */     }
/*     */     
/*     */     public void preModify(Account acct, String newPassword, Map context, Map<String, Object> attrsToModify) throws ServiceException
/*     */     {
/* 308 */       this.preModifyCalled = Boolean.valueOf(true);
/* 309 */       this.acctParam = acct;
/* 310 */       this.newPasswordParam = newPassword;
/* 311 */       context.put("NEW", newPassword);
/*     */     }
/*     */     
/*     */     public void postModify(Account acct, String newPassword, Map context)
/*     */     {
/* 316 */       this.postModifyCalled = Boolean.valueOf(true);
/*     */     }
/*     */     
/*     */     public void onException(Account acct, String newPassword, Map context, ServiceException se)
/*     */     {
/* 321 */       this.onExceptionCalled = Boolean.valueOf(true);
/*     */       
/* 323 */       Assert.assertTrue(se instanceof ServiceException);
/* 324 */       Assert.assertTrue(se.getMessage().equals("forbidden: password modification is not permitted for this account"));
/*     */       
/* 326 */       Assert.assertEquals(acct, this.acctParam);
/* 327 */       Assert.assertEquals(newPassword, this.newPasswordParam);
/* 328 */       Assert.assertEquals(newPassword, context.get("NEW"));
/*     */     }
/*     */     
/*     */     public Boolean isPreModifyCalled()
/*     */     {
/* 333 */       return this.preModifyCalled;
/*     */     }
/*     */     
/*     */     public Boolean isPostModifyCalled() {
/* 337 */       return this.postModifyCalled;
/*     */     }
/*     */     
/*     */     public Boolean isOnExceptionCalled() {
/* 341 */       return this.onExceptionCalled;
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/prov/ldap/TestChangePasswordListener.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */